/**
 * 角色页
 */
import React from 'react';
import { connect } from 'dva';
import { Row, Col, Select, Popconfirm, Button } from 'antd';

import shouldComponentUpdate from '../../components/decorator/shouldComponentUpdate';
import Table from './table';
import Filter from './filter';
import getPagination from '../../components/pagination';
import DigitalBoard from '../../components/digitalBoard';
import { dayToDateString } from '../../components/dateRange';
import { devStatusRate } from '../../services/testDevice';
import showInHeader from '../../components/decorator/showInHeader';

const Option = Select.Option;
const options = [
	{ value: 1, name: '最近一天' },
	{ value: 7, name: '最近7天' },
	{ value: 30, name: '最近30天' },
];

function toPercent(data) {
	return `${((data || 0) * 100).toFixed(1)}%`;
}
const types = [
	{
		type: 'device',
		name: '测试通过率',
		query: devStatusRate,
		dataKey: data => toPercent(data.successRate)
	},
	{
		type: 'device',
		name: '测试失败率',
		query: devStatusRate,
		dataKey: data => toPercent(data.failRate)
	},
];

const styles = {
  defaultV: {
    color: '#ccc'
  }
};

const columns = {
  //WSH6100N V2.1.1
  'da3cb6515ebd4236bd69c6f5148753df': [
    'productName',
    'imei',
    'username',
    'signalStrength',
    'signalSNR',
    'linkQuality',
    'signalECL',
    'batteryLevel',
    'manufacturer',
    'normal',
    'sensorFault',
    'fault',
    'pvc',
    'result',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'onlineStatus',
  ],
  // 'da3cb6515ebd4236bd69c6f5148753df': [
  //   'index',
  //   'imei',
  //   'signalStrength',
  //     'iccid',
  //   'signalSNR',
  //   'linkQuality',
  //   'batteryLevel',
  //   'manufacturer',
  //   'normal',
  //   'sensorFault',
  //   'fault',
  //   'pvc',
  //   'signalECL',
  //   'undervoltageFault',
  //   'pollutionFault',
  //   'staticPointFault',
  //   'detectorLossFault',
  //   'fireAlarm',
  //   'rssi',
  //   'selfChecking',
  //   'analogQuantity',
  //   'temperature',
  //   "statusTime",
  //   'result',
  //   'opt',
  // ],
  //VH75规格NB V0.3.0
  '3abb1ae62b9b494da1ceafeb51165469': [
    'productName',
    'imei',
    'username',
    'result',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'undervoltageFaultModule',
    'onlineStatus',
  ],
   //VT3431WN V0.1.0
  '6a1ae2b0f5b84c4ea6807951561c725a': [
    'productName',
    'imei',
    'username',
    'result',
    'undervoltageFault',
    'fireAlarm',
    'rssi',
    'analogQuantity',
    'onlineStatus',
  ],
   //WSH6100N V1.1.0
  'fa1c1dc4df9b4a3ca0dac26308ac3836': [
    'productName',
    'imei',
    'username',
    'result',
    'undervoltageFault',
    'pollutionFault',
    'staticPointFault',
    'detectorLossFault',
    'fireAlarm',
    'rssi',
    'selfChecking',
    'analogQuantity',
    'temperature',
    'undervoltageFaultModule',
    'wirelessmoduleVersion',
    'modulesoftwareVersion',
    'deviceVersion',
    'wirelessmoduleElectricity',
    'componetType',
    'deviceStatus',
    'devicesEvent',
    'packNumber',
    'ccid',
    'controlOlder',
    'onlineStatus',
  ],

   //VT3431WN V0.2.0
  'a9ad4f923632415a8c6a7ac65b4f61bc': [
    'productName',
    'imei',
    'username',
    'result',
    'undervoltageFault',
    'fireAlarm',
    'rssi',
    'analogQuantity',
    'remote_muffler',
    'remote_selftest',
    'onlineStatus',
  ],
   //VH75规格NB  V0.1.0
  '7522fab06c4841c883eca4012834e791':[
    'productName',
    'imei',
    'username',
    'result',
    'undervoltageFault',
    'fireAlarm',
    'rssi',
  ],
  '93c1539d58d34e9c9868cfde8c6c23df':[
    'productName',
    'imei',
    'username',
    'signalStrength',
    'signalSNR',
    'linkQuality',
    'batteryLevel',
    'manufacturer',
    'normal',
    'sensorFault',
    'fault',
    'pvc',
  ]

};

const columnsItems = [
  // {
  //   title: '序号',
  //   dataIndex: 'index',
  //   key: 'index',
  //   width:'35px',
  //   fixed: 'left',
  // },
  {
    title: '产品名称',
    dataIndex: 'productName',
    key: 'productName',
  },
  {
    title: 'IMEI号',
    dataIndex: 'imei',
    key: 'imei',
  },
  {
    title: '测试人员',
    dataIndex: 'username',
    key: 'username',
  },
  {
    title:"iccid",
    dataIndex:"iccid",
    key:"iccid",
  },
  {
    title:"信号覆盖等级",
    dataIndex:"signalECL",
    key:"signalECL",
  },
  // {
  //   title:"时间",
  //   dataIndex:"statusTime",
  //   key:"statusTime",
  // },
  {
    title: '测试状态',
    dataIndex: 'result',
    key: 'result',
  },
  {
    title: '测试时间',
    dataIndex: 'testTime',
    key: 'testTime',
  },
  {
    title: '电量',
    dataIndex: 'batteryLevel',
    key: 'batteryLevel',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '信号强度',
    dataIndex: 'signalStrength',
    key: 'signalStrength',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '信噪比',
    dataIndex: 'signalSNR',
    key: 'signalSNR',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '连接质量',
    dataIndex: 'linkQuality',
    key: 'linkQuality',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  }, {
    title: '厂商代码',
    dataIndex: 'manufacturer',
    key: 'manufacturer',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '正常',
    dataIndex: 'normal',
    key: 'normal',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '传感器故障',
    dataIndex: 'sensorFault',
    key: 'sensorFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },{
    title: '故障',
    dataIndex: 'fault',
    key: 'fault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },{
    title: '电量不足',
    dataIndex: 'pvc',
    key: 'pvc',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '欠压故障',
    dataIndex: 'undervoltageFault',
    key: 'undervoltageFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span> == '是' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '污染故障',
    dataIndex: 'pollutionFault',
    key: 'pollutionFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '静态点故障',
    dataIndex: 'staticPointFault',
    key: 'staticPointFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '丢失故障',
    dataIndex: 'detectorLossFault',
    key: 'detectorLossFault',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '火警',
    dataIndex: 'fireAlarm',
    key: 'fireAlarm',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '自检',
    dataIndex: 'selfChecking',
    key: 'selfChecking',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  },
  {
    title: '模组欠压故障',
    dataIndex: 'undervoltageFaultModule',
    key: 'undervoltageFaultModule',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag == 'true' ? <span style={styles.colorR}>是</span> : <span style={styles.colorG}>否</span>
  }, {
    title: '模拟量',
    dataIndex: 'analogQuantity',
    key: 'analogQuantity',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '温度',
    dataIndex: 'temperature',
    key: 'temperature',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: 'RSSI',
    dataIndex: 'rssi',
    key: 'rssi',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '无线模组版本号',
    dataIndex: 'wirelessmoduleVersion',
    key: 'wirelessmoduleVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '模组固件版本号',
    dataIndex: 'modulesoftwareVersion',
    key: 'modulesoftwareVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备版本号',
    dataIndex: 'deviceVersion',
    key: 'deviceVersion',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '无线模组电量',
    dataIndex: 'wirelessmoduleElectricity',
    key: 'wirelessmoduleElectricity',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '部件类型',
    dataIndex: 'componetType',
    key: 'componetType',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备状态',
    dataIndex: 'deviceStatus',
    key: 'deviceStatus',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '设备事件',
    dataIndex: 'devicesEvent',
    key: 'devicesEvent',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '包序号',
    dataIndex: 'packNumber',
    key: 'packNumber',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: 'CCID',
    dataIndex: 'ccid',
    key: 'ccid',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '控制指令',
    dataIndex: 'controlOlder',
    key: 'controlOlder',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '远程消音',
    dataIndex: 'remote_muffler',
    key: 'remote_muffler',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  }, {
    title: '远程自检',
    dataIndex: 'remote_selftest',
    key: 'remote_selftest',
    render: flag => (flag == '暂无数据' || !flag) ? <span style={styles.defaultV}>暂无数据</span> : flag
  },
  {
    title: '在线状态',
    dataIndex: 'onlineStatus',
    key: 'onlineStatus'
  }
];

@showInHeader(['productSelect'])
@shouldComponentUpdate
class TestResult extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      queryData: {
        current: 1,
        size: 10,
        query: {
            condition: '',
            endTime: '',
            result: 3,
            startTime: '',
            type: 1
          },
			},
			rateQueryData: {
				startTime: dayToDateString(1)[0],
				endTime: dayToDateString(1)[1]
			}
    };
  }

  query = () => {
    const { dispatch } = this.props;
    const { queryData } = this.state;
    dispatch({
      type: 'testResult/query',
      payload: queryData,
    });
  }

  onSelect = keys => {
    this.props.dispatch({
      type: 'testResult/set',
      payload: {
        selectedRowKeys: keys
      }
    });
  }

  queryChange = (query) => {
    const { queryData } = this.state;
    this.setState({
      queryData: {
        ...queryData,
        query: {
          ...query,
        }
      }
    }, this.query);
  }

  onPageChange = (current) => {
    const { dispatch } = this.props;
    const { queryData } = this.state;
    dispatch({
      type: 'testResult/query',
      payload: {
        ...queryData,
        current,
      }
    });
  }

  onShowSizeChange = (current, size) => {
    const { dispatch } = this.props;
    const { queryData } = this.state;
    dispatch({
      type: 'testResult/query',
      payload: {
        ...queryData,
        size,
      }
    });
	}
	selectChange = v => {
		const [startTime, endTime] = dayToDateString(v);
		const rateQueryData = { startTime, endTime };
		this.setState({ rateQueryData });
  }

  render() {
		const { rateQueryData } = this.state;
    const { selectedRowKeys, list, total, current } = this.props.testResult;
    const { productList, productId} = this.props;
    const currentProduct = productList.find((item) => {
      return item.id === productId;
    }) || {};
    // console.log(productList)
    const productName = currentProduct.name;
    const productKey = currentProduct.gizwitsProductKey || 'da3cb6515ebd4236bd69c6f5148753df';
    // console.log(productKey)
    const currentCol = columns[productKey].map((item) => {
      return columnsItems.find(colItem => colItem.key === item);
    });

    return (
      <div className="content-inner">
				<Row gutter={16} >
					{
						types.map(t => (
							<Col className="gutter-row" span={8}>
								<DigitalBoard {...t} queryData={{ ...rateQueryData, productKey }} />
							</Col>
						))
					}
					<Col className="gutter-row" span={8}>
						<Select onChange={this.selectChange} style={{ width: '10em' }} defaultValue={1}>{options.map(o => <Option value={o.value}>{o.name}</Option>)}</Select>
					</Col>
				</Row>
        <Filter
          keys={selectedRowKeys}
          query={this.queryChange}
          exportTitle="testResultExport"
          productName={productName}
          productKey={productKey}
          currentCol={currentCol}
        />
        <Table
          list={list}
          pagination={getPagination({
            total,
            current,
            onChange: this.onPageChange,
            onShowSizeChange: this.onShowSizeChange
          })}
          select={this.onSelect}
          selectKeys={selectedRowKeys}
          exportTitle="testResultExport"
          productKey={productKey}
          currentCol={currentCol}
        />
      </div>
    );
  }
}

export default connect(
  ({ testResult, loading, app: { productId }, productList: { list: productList } }) => ({ testResult, loading, productId, productList })
)(TestResult);
